package com.blast.sspai.util;

/**
 * Created by dell on 2015/6/17.
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

/**
 * 用户处理Volley异步加载图片的工具类
 * 单例模式 在Applicationcontext里加载
 */
public class RequestManager {
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;
    private static RequestManager ourInstance;
    public static RequestManager getInstance(){
        return ourInstance;
    }
    public static RequestManager createInstance(Context context){
        if (context != null) {
            if (ourInstance == null) {
                ourInstance = new RequestManager(context);
            }
        }else {
            throw new IllegalArgumentException("Context must be set");
        }

        return  ourInstance;
    }

    private RequestManager(Context context) {
        requestQueue = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {//内部接口（类）

            private LruCache<String,Bitmap> lruCache = new LruCache<String, Bitmap>(20);

            @Override
            public Bitmap getBitmap(String url) {
                return lruCache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                lruCache.put(url,bitmap);
            }
        }
        );
    }

    public ImageLoader getImageLoader() {
        return imageLoader;
    }

    public RequestQueue getRequestQueue(){
        return requestQueue;
    }

}
